package jp.sourceforge.akjrcp.util;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.WorkbenchWindow;

public class ProgressMonitorUtils {

	//work only main-thread.
	public static IProgressMonitor getProgressMonitor() {
		WorkbenchWindow workbenchWindow = (WorkbenchWindow)PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		return workbenchWindow.getStatusLineManager().getProgressMonitor();
	}
	public static void beginMonitor(IProgressMonitor monitor,String text,int step){
		beginMonitor(monitor,text,step,true);
	}
	public static void beginMonitor(IProgressMonitor monitor,String text,int step,boolean sync){
		if(sync){
				PlatformUI.getWorkbench().getDisplay().syncExec(new ProgressMonitorBegin(monitor,text,step));
		}else{
			
				PlatformUI.getWorkbench().getDisplay().asyncExec(new ProgressMonitorBegin(monitor,text,step));
			
		}
	}
	
	public static void subtaskMonitor(IProgressMonitor monitor,String text){
		subtaskMonitor(monitor,text);
	}
	public static void subtaskMonitor(IProgressMonitor monitor,String text,boolean sync){
		if(sync){
				PlatformUI.getWorkbench().getDisplay().syncExec(new ProgressMonitorSubtask(monitor,text));
		}else{
			
				PlatformUI.getWorkbench().getDisplay().asyncExec(new ProgressMonitorSubtask(monitor,text));
			
		}
	}
	
	public static void doneMonitor(IProgressMonitor monitor){
		doneMonitor(monitor,true);
	}
	public static void doneMonitor(IProgressMonitor monitor,boolean sync){
		if(sync){
				PlatformUI.getWorkbench().getDisplay().syncExec(new ProgressMonitorDone(monitor));
		}else{
				PlatformUI.getWorkbench().getDisplay().asyncExec(new ProgressMonitorDone(monitor));
			
		}
	}
	
	public static void workedMonitor(IProgressMonitor monitor,int step){
		workedMonitor(monitor,step,true);
	}
	public static void workedMonitor(IProgressMonitor monitor,int step,boolean sync){
		if(sync){
				PlatformUI.getWorkbench().getDisplay().syncExec(new ProgressMonitorWorked(monitor,step));
		}else{
			
				PlatformUI.getWorkbench().getDisplay().asyncExec(new ProgressMonitorWorked(monitor,step));
			
		}
	}

	private static class ProgressMonitorDone implements Runnable {
		IProgressMonitor monitor;

		public ProgressMonitorDone(IProgressMonitor monitor) {
			this.monitor = monitor;

		}

		public void run() {
			monitor.done();
		}
	}

	private static  class ProgressMonitorBegin implements Runnable {
		IProgressMonitor monitor;

		String text;

		int step;

		public ProgressMonitorBegin(IProgressMonitor monitor, String text,
				int step) {
			this.monitor = monitor;
			this.text = text;
			this.step = step;
		}

		public void run() {
			
			monitor.beginTask(text, step);
			
		}
	}
	
	
	private static  class ProgressMonitorWorked implements Runnable {
		IProgressMonitor monitor;

		

		int step;

		public ProgressMonitorWorked(IProgressMonitor monitor,
				int step) {
			this.monitor = monitor;
			
			this.step = step;
		}

		public void run() {
			//log.infoworked:"+step);
			monitor.worked(step);
			
		}
	}
	
	private static  class ProgressMonitorSubtask implements Runnable {
		IProgressMonitor monitor;

		String text;


		public ProgressMonitorSubtask(IProgressMonitor monitor, String text
				) {
			this.monitor = monitor;
			this.text = text;
			
		}

		public void run() {
			
			monitor.subTask(text);
			
		}
	}
}
